<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: account_edit.php 438 2014-01-19 11:50:30Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 438 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2014-01-19 11:50:30 +0000 (Sun, 19 Jan 2014) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */
 define('GLOBAL_COLUMN_LEFT', '0');
include ('includes/application_top.php');

if(!isset($_SESSION['customer_id']))
	redirect(href_link(FILENAME_LOGIN, '', 'SSL'));

if($_SESSION['customers_status']['customers_status_id'] == 0)
	redirect(href_link_admin(FILENAME_CUSTOMERS, 'cID='.$_SESSION['customer_id'].'&action=edit', 'SSL'));

$smarty = new Smarty;

require (DIR_FS_CATALOG.'templates/'.CURRENT_TEMPLATE.'/source/boxes.php');

require_once (DIR_FS_INC.'inc.date_short.php');
require_once (DIR_FS_INC.'inc.image_button.php');
require_once (DIR_FS_INC.'inc.validate_email.php');
require_once (DIR_FS_INC.'inc.get_geo_zone_code.php');
require_once (DIR_FS_INC.'inc.get_customers_country.php');

require(DIR_WS_CLASSES.'class.input_fields.php');

if(isset($_POST['action']) && ($_POST['action'] == 'process')) {
	
	$field = new input_fields('create_account', $_POST);

	$gender_male = $field->getField('gender', 'm');
	$gender_female = $field->getField('gender', 'f');
	$firstname = $field->getField('firstname');
	$lastname = $field->getField('lastname');
	$dob = $field->getField('dob');
	$email_address = $field->getField('email');
	$vat = $field->getField('vat');
	$telephone = $field->getField('telephone');
	$fax = $field->getField('fax');
	
	if($field->error != 'true') {
		$sql_data_array = array('customers_vat_id' => $field->post_vars['vat'],
								'customers_vat_id_status' => $field->customers_vat_id_status,
								'customers_firstname' => (!empty($field->post_vars['title']) ? $field->post_vars['title'].' ' : '' ).$field->post_vars['firstname'],
								'customers_lastname' => $field->post_vars['lastname'], 
								'customers_email_address' => $field->post_vars['email'],
								'customers_telephone' => $field->post_vars['telephone'],
								'customers_fax' => $field->post_vars['fax'],
								'customers_last_modified' => 'now()');

		if (!empty($field->post_vars['gender']))
			$sql_data_array['customers_gender'] = $field->post_vars['gender'];
		if (!empty($field->post_vars['dob']))
			$sql_data_array['customers_dob'] = date_raw($field->post_vars['dob']);

		$db->db_perform(TABLE_CUSTOMERS, $sql_data_array, 'update', "customers_id = '".(int)$_SESSION['customer_id']."'");

		$db->db_query("UPDATE 
							".TABLE_CUSTOMERS_INFO." 
						SET 
							customers_info_date_account_last_modified = NOW() 
						WHERE 
							customers_info_id = '".(int)$_SESSION['customer_id']."'");

		$customer_first_name = $firstname;
		$message_stack->add_session(SUCCESS_ACCOUNT_UPDATED, 'success');
		redirect(href_link(FILENAME_ACCOUNT, '', 'SSL'));
	}
		
} else {
	$account = $db->db_query("SELECT 
									customers_gender, 
									customers_cid, 
									customers_vat_id, 
									customers_vat_id_status, 
									customers_firstname, 
									customers_lastname, 
									customers_dob, 
									customers_email_address, 
									customers_telephone, 
									customers_fax 
								FROM 
									".TABLE_CUSTOMERS." 
								WHERE 
									customers_id = '".(int)$_SESSION['customer_id']."'");
									
	$field = new input_fields('create_account');

	$gender_male = $field->getField('gender', 'm', ($account->fields['customers_gender'] == 'm' ? ' checked=""' : ''));
	$gender_female = $field->getField('gender', 'f', ($account->fields['customers_gender'] == 'f' ? ' checked=""' : ''));
	$firstname = $field->getField('firstname', $account->fields['customers_firstname']);
	$lastname = $field->getField('lastname', $account->fields['customers_lastname']);
	$dob = $field->getField('dob', $account->fields['customers_dob']);
	$email_address = $field->getField('email', $account->fields['customers_email_address']);
	$vat = $field->getField('vat', $account->fields['customers_vat_id']);
	$telephone = $field->getField('telephone', $account->fields['customers_telephone']);
	$fax = $field->getField('fax', $account->fields['customers_fax']);
}

$breadcrumb->add(NAVBAR_TITLE_1_ACCOUNT_EDIT, href_link(FILENAME_ACCOUNT, '', 'SSL'));
$breadcrumb->add(NAVBAR_TITLE_2_ACCOUNT_EDIT, href_link(FILENAME_ACCOUNT_EDIT, '', 'SSL'));

require (DIR_WS_INCLUDES.'header.php');
$smarty->assign('FORM_ACTION', draw_form('account_edit', href_link(FILENAME_ACCOUNT_EDIT, '', 'SSL'), 'post', 'onsubmit="return check_form(account_edit);"').draw_hidden_field('action', 'process'));

$smarty->assign('INPUT_MALE', $gender_male);
$smarty->assign('INPUT_FEMALE', $gender_female);
$smarty->assign('INPUT_FIRSTNAME', $firstname);
$smarty->assign('INPUT_LASTNAME', $lastname);
$smarty->assign('INPUT_DOB', $dob);
$smarty->assign('INPUT_EMAIL', $email_address);
$smarty->assign('INPUT_TEL', $telephone);
$smarty->assign('INPUT_FAX', $fax);
$smarty->assign('INPUT_VAT', $vat);

$smarty->assign('BUTTON_BACK', '<a href="'.href_link(FILENAME_ACCOUNT, '', 'SSL').'">'.image_button('button_back.gif', IMAGE_BUTTON_BACK).'</a>');
$smarty->assign('BUTTON_SUBMIT', image_submit('button_continue.gif', IMAGE_BUTTON_CONTINUE));
$smarty->assign('FORM_END', '</form>');
$smarty->assign('language', $_SESSION['language']);

$smarty->caching = false;
$main_content = $smarty->fetch(CURRENT_TEMPLATE.'/module/account_edit.html');

$smarty->assign('language', $_SESSION['language']);
$smarty->assign('main_content', $main_content);
$smarty->caching = false;
if(!defined('RM'))
	$smarty->loadFilter('output', 'note');
$smarty->loadFilter('output','trimwhitespace');
$smarty->display(CURRENT_TEMPLATE.'/index.html');
include ('includes/application_bottom.php');
?>